function [ delta_weights ] = back_propagation( weights, deltas, h, pattern )

global LAYERS;
global ETA;
%BACK_PROPAGATION
%   Esta funcion calcula los deltas de los pesos de cada capa y los
%   devuelve en un vector

j = length(LAYERS);
o = cell(1, length(LAYERS)-1);
for k = 1:length(LAYERS)-1
    o{k} = vertcat(-1, g_function(h{j-1}));
    deltas{k+1} = g_function_p(o{k}) .* (weights{j}' * deltas{k});
    deltas{k+1} = deltas{k+1}(2:length(deltas{k+1}),:);
    j = j - 1;
end
deltas = fliplr(deltas);
o = fliplr(o);
delta_weights = cell(1, length(deltas));
delta_weights{1} = ETA * deltas{1} * pattern';
for i = 2:length(deltas)
   delta_weights{i} = ETA * deltas{i} * o{i-1}';
end

end

